package cn.com.gridinfo.sooc.core.expand.callname.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jeremy.arad.utils.Log;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerviewViewHolder;
import com.marshalchen.ultimaterecyclerview.UltimateViewAdapter;

import java.util.List;

import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.core.expand.callname.activity.StudentMenuActivity;
import cn.com.gridinfo.sooc.core.expand.callname.bean.ClassInfo;

import static android.view.GestureDetector.*;

/**
 * 课程名单界面列表适配器
 *
 * @author yingchunli
 */
public class CourseMenuAdapter extends UltimateViewAdapter<CourseMenuAdapter.ViewHolder> {

    public List<ClassInfo> classInfoList;
    Context context;
    ViewGroup viewGroup;

    public CourseMenuAdapter(List<ClassInfo> classInfoList, Context context) {
        this.classInfoList = classInfoList;
        this.context = context;
    }

    public void loadmore(List<ClassInfo> list) {
        classInfoList.addAll(list);
        notifyDataSetChanged();
    }

    public void refresh(List<ClassInfo> list) {
        classInfoList.clear();
        classInfoList.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        return null;
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    public ViewHolder getViewHolder(View view) {
        return new ViewHolder(view, false);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent) {
        //加载布局
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.expand_callname_coursemenu_item, parent, false);
        ViewHolder holder = new ViewHolder(view, true);
        view.setOnClickListener(holder);
        view.setOnLongClickListener(holder);
        viewGroup = parent;
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        if (position < getItemCount() &&
                (customHeaderView != null ? position <= classInfoList.size() : position < classInfoList.size())
                && (customHeaderView != null ? position > 0 : true)) {
            final ClassInfo classInfo = classInfoList.get(position);

            holder.classname.setText(classInfo.getKcmc() + "(" + classInfo.getSkxq() + ")");
            String str = classInfo.getKcmc() + "(" + classInfo.getSkxq() + ")";
            //设置字体颜色
            SpannableStringBuilder builder = new SpannableStringBuilder(holder.classname.getText().toString());
            builder.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.color_tv333)),
                    0, classInfo.getKcmc().length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            builder.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.color_666)),
                    classInfo.getKcmc().length(), str.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
            holder.classname.setText(builder);

            holder.mdrs.setText("共" + classInfo.getXsrs() + "名学生");


            //底部分割线的显示
//            if (position == classInfoList.size() - 1) {
//                holder.line.setVisibility(View.VISIBLE);
//            } else {
//                holder.line.setVisibility(View.GONE);
//            }
        }
    }

    /**
     * 删除课程
     */
    public void delete(int positon) {
        classInfoList.remove(positon);
        notifyDataSetChanged();
    }

    /**
     * 点击事件接口
     */
    public interface OnItemClickListener {

        void OnItemClick(ClassInfo classInfo, int position);

        void OnItemClick(ClassInfo classInfo);
    }

    private OnItemClickListener mOnItemClickLitener;

    public void setmOnItemClickLitener(OnItemClickListener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    @Override
    public int getAdapterItemCount() {
        return classInfoList.size();
    }

    @Override
    public long generateHeaderId(int position) {
        return position;
    }

    //ViewHolder
    public class ViewHolder extends UltimateRecyclerviewViewHolder implements View.OnClickListener, View.OnLongClickListener {
        TextView classname, mdrs;
        View line;


        public ViewHolder(View itemView, boolean isItem) {
            super(itemView);
            if (isItem) {
                classname = (TextView) itemView.findViewById(R.id.className_callname_coursemenu);
                mdrs = (TextView) itemView.findViewById(R.id.xsrs_callname_coursemenu);

//                line = itemView.findViewById(R.id.line_bottom);
            }
        }

        @Override
        public void onClick(View v) {
            //跳转到学生名单页面
            mOnItemClickLitener.OnItemClick(classInfoList.get(getAdapterPosition()));
        }

        @Override
        public boolean onLongClick(View v) {
            //编辑课程
            mOnItemClickLitener.OnItemClick(classInfoList.get(getAdapterPosition()), getAdapterPosition());
            return false;
        }
    }
}
